using FSM.IDal; 
using FSM.IBll; 
using FSM.Model; 
using SqlSugar; 
using System; 
using System.Collections.Generic; 
using System.Text;
using System.Linq;
using Util.Extensions;

namespace FSM.Bll 
{ 
    public class RelUserRoleBll: IRelUserRoleBll 
    { 
        #region 模块 
        private readonly IRelUserRoleDal dal; 
        public RelUserRoleBll(IRelUserRoleDal dal) 
        { 
            this.dal = dal; 
        } 
        #endregion 
 
        #region 查询 
 
        /// <summary> 
        /// 根据键值查询单个信息 
        /// </summary> 
        /// <param name="pkid">键值</param> 
        /// <returns></returns> 
        public rel_user_role Get(string pkid) 
        { 
            return dal.GetById(pkid); 
        } 
 
        /// <summary> 
        /// 查询批量信息(分页)
        /// </summary> 
        /// <param name="user_no">用户编号</param> 
        /// <param name="role_no">角色编号</param> 
        /// <param name="page">页码</param> 
        /// <param name="page_size">每页显示的条数</param> 
        /// <returns></returns> 
        public Tuple<List<rel_user_role>, int> ListPagin(string user_no, string role_no, int page, int page_size) 
        { 
            ISugarQueryable<rel_user_role> queryable = dal.CreateQueryable(); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(user_no),p => p.user_no == user_no); 
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(role_no),p => p.role_no == role_no); 
             
            PageModel pm = new PageModel 
            { 
                PageIndex = page, 
                PageSize = page_size 
            }; 
            Tuple<List<rel_user_role>, int> result = dal.GetPageListAndTotal(queryable, pm); 
            return result; 
        }

        /// <summary>
        /// 查询批量信息
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <param name="role_no">角色编号</param>
        /// <returns></returns>
        public List<rel_user_role> List(string user_no, string role_no)
        {
            Expressionable<rel_user_role> queryable = Expressionable.Create<rel_user_role>();
            queryable = queryable.AndIF(!string.IsNullOrEmpty(user_no), p => p.user_no == user_no);
            queryable = queryable.AndIF(!string.IsNullOrEmpty(role_no), p => p.role_no == role_no);

            List<rel_user_role> result = dal.GetList(queryable.ToExpression());
            return result;
        }

        /// <summary>
        /// 判断用户角色对应关系是否存在
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <param name="role_no">角色编号</param>
        /// <returns></returns>
        public bool Exists(string user_no, string role_no)
        {
            return dal.GetCount(p => p.user_no == user_no && p.role_no == role_no) > 0;
        }

        /// <summary>
        /// 是否超级管理员组的成员
        /// </summary>
        /// <param name="user_no">用户编号</param>
        /// <returns></returns>
        public bool IsAdministrators(string user_no)
        {
            return Exists(user_no, "Administrators");
        }
 
        #endregion 
 
        #region 提交 
 
        /// <summary> 
        /// 添加 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Insert(rel_user_role model) 
        { 
            model.create_date = DateTime.Now; 
            return dal.Insert(model); 
        }

        /// <summary>
        /// 批量插入数据
        /// </summary>
        /// <param name="oper">操作人员</param>
        /// <param name="user_no">用户编号</param>
        /// <param name="role_nos">角色编号列表</param>
        /// <returns></returns>
        public bool BatchInsert(string oper, string user_no, List<string> role_nos)
        {
            dal.BeginTran();
            try
            {
                oper.ValidEmptyThrowException("操作人员");
                user_no.ValidEmptyThrowException("要操作的用户");
                if (oper == user_no)
                {
                    throw new Exception("不能操作当前登录人员的权限");
                }

                DateTime dtNow = new DateTime();
                bool success = false;

                //查询用户原来拥有的菜单信息
                List<rel_user_role> userRoles = dal.GetList(p => p.user_no == user_no);
                IEnumerable<string> oldRoleNos = userRoles.Select(p => p.role_no);

                //删除原来拥有现在没有角色信息
                IEnumerable<string> deletePkids = userRoles.Where(p => role_nos.Contains(p.role_no) == false).Select(p=>p.pkid);
                if (deletePkids.Count() > 0)
                {
                    success = dal.Delete(deletePkids.ToArray());
                    if (success == false)
                    {
                        throw new Exception("移除原有角色信息失败");
                    }
                }

                //添加新增的角色信息
                IEnumerable<string> newRoleNos = role_nos.Where(p => oldRoleNos.Contains(p) == false);
                if (newRoleNos.Count() > 0)
                {
                    List<rel_user_role> newModels = new List<rel_user_role>();
                    foreach (string no in newRoleNos)
                    {
                        newModels.Add(new rel_user_role()
                        {
                            pkid = Guid.NewGuid().ToString("N"),
                            user_no = user_no,
                            role_no = no,
                            creater = oper,
                            create_date = dtNow
                        });
                    }
                    success = dal.Insert(newModels);
                    if (success == false)
                    {
                        throw new Exception("新增角色信息失败");
                    }
                }

                dal.CommitTran();
                return true;
            }
            catch (Exception)
            {
                dal.RollbackTran();
                throw;
            }
        }

        /// <summary> 
        /// 更新 
        /// </summary> 
        /// <param name="model">实体</param> 
        /// <returns></returns> 
        public bool Update(rel_user_role model) 
        { 
            model.edit_date = DateTime.Now; 
            return dal.UpdateExcludeColumns(model, "creater", "create_date") == 1; 
        } 
 
        /// <summary> 
        ///  删除 
        /// </summary> 
        /// <param name="pkid">键值</param> 
        /// <returns></returns> 
        public bool Delete(params string[] pkid_arr) 
        { 
            return dal.Delete(pkid_arr); 
        } 
        #endregion 
    } 
} 
